package com.kgc.crowd.distributioncrowdwebui.controller;

import com.kgc.crowd.distributioncrowdcommon.api.MemberManagerRemoteService;
import com.kgc.crowd.distributioncrowdcommon.api.RedisOperationRemoteService;
import com.kgc.crowd.distributioncrowdcommon.pojo.ResultEntity;
import com.kgc.crowd.distributioncrowdcommon.util.CrowdConstant;
import com.kgc.crowd.distributioncrowdcommon.vo.MemberSignSuccessVO;
import com.kgc.crowd.distributioncrowdcommon.vo.MemberVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
@RequestMapping("/member/manager")
@Controller
public class MemberController {
    @Resource
    MemberManagerRemoteService
            memberManagerRemoteService;
    @Resource
    RedisOperationRemoteService redisOperationRemoteService;

    /**
     * 登录
     * @param memberVO
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/do/login")
    public String login(MemberVO memberVO, Model model, HttpSession session){
        String loginacct = memberVO.getLoginacct();
        String userpswd = memberVO.getUserpswd();
        System.out.println("name:"+loginacct+",密码:"+userpswd);
        ResultEntity<MemberSignSuccessVO> login = memberManagerRemoteService.login(loginacct, userpswd);
        if(login.getResult().equals(ResultEntity.FAILED)){
            model.addAttribute(CrowdConstant.ATTR_NAME_MESSAGE,CrowdConstant.MESSAGE_LOGIN_FAILED);
            return "login";
        }
        MemberSignSuccessVO data = login.getData();
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER,data);
        return "member";
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        MemberSignSuccessVO memberSignSuccessVO = (MemberSignSuccessVO) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        if(memberSignSuccessVO==null){
            return "redirect:/index";
        }
        String token = memberSignSuccessVO.getToken();
        ResultEntity<String> logout = memberManagerRemoteService.logout(token);
        if(ResultEntity.FAILED.equals(logout.getResult())){
            throw new RuntimeException(logout.getMessage());
        }
        session.invalidate();
        return "redirect:/index";
    }

    /**
     * 获取验证码
     * @param phoneNum
     * @return
     */
    @RequestMapping("/send/code")
    @ResponseBody
    public ResultEntity<String> sendCode(String phoneNum){
        ResultEntity<String> stringResultEntity = memberManagerRemoteService.sendCode(phoneNum);
        return stringResultEntity;
    }

    /**
     * 注册
     */
    @RequestMapping("/register")
    public String register(MemberVO memberVO,Model model){
        System.out.println("尽方法refister");
        ResultEntity<String> register = memberManagerRemoteService.register(memberVO);
        if(register.getResult().equals(ResultEntity.FAILED)){
            System.out.println("n");
            model.addAttribute(CrowdConstant.ATTR_NAME_MESSAGE,register.getMessage());
            return "reg";
        }
        System.out.println("y");
        return "login";
    }

    @RequestMapping("/minecrowdfunding")
    public String tominecrowdfunding(){
        return "minecrowdfunding";
    }
    @RequestMapping("/member")
    public String tomember(){
        return "member";
    }

}
